import os
from urllib.request import urlopen
from bs4 import BeautifulSoup

first = 800
last = 895
Lists = {}

pwd = os.path.join(os.getcwd(),"problems")

url = "http://codeforces.com/contest/"

for i in range(first,last+1):

    this_url = url + str(i)
    html = urlopen(this_url)
    bsobj = BeautifulSoup(html,'html.parser')
    name = str(bsobj.find("table",{"class":"rtable"}).tr.get_text().strip())
    print(name)

    this_pwd = os.path.join(pwd , name)
    if not os.path.exists(this_pwd):
        os.mkdir(this_pwd)
    for k in ['A','B','C']:
        get_new_url = url + str(i) + '/problem/' + k
        html = urlopen(get_new_url)
        bsobj = BeautifulSoup(html,'html.parser')

        body = bsobj.find("div",{"class":"problemindexholder"})

        tags = bsobj.findAll("span",{"class":"tag-box"})

        for t in tags:
            t_mean = t.get_text().strip()
            if t_mean not in Lists.keys() :
                Lists[t_mean]=[]
            Lists[t_mean].append(name+k);

        f = open(os.path.join(this_pwd,k + ".html"),"w")
        try:
            f.write(str(body.prettify()))
        except:
            print("Can'nt Write")
        f.close()

    print("finish!" + str(i) )

f = open(os.path.join(pwd,"list"),"w")

for name,l in Lists.items():
    f.write(name+"\n")
    for i in l:
        f.write("    "+ i + "\n")
    f.write("\n")

f.close()

